package com.example.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;


//mapper阶段代码
//k1 是mapper的键输出类型

public class WordCountMapper extends
        Mapper<LongWritable, Text,Text, IntWritable>{
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        //获取到map输入的值 ，需要将Text转String
        String lines = value.toString();

        //将获取到的这行文本拆分出单词
        String[] words = lines.split(" ");
        //Hello world -> {“Hello”,"world"}
        //从数组中遍历，并计数
        for (String word: words){
            //map阶段的输出，k2，v2 ： context.write\
             //k2
            Text k2 = new Text(word);
            IntWritable v2 = new IntWritable();
            //每个单词计数1
            v2.set(1);
            context.write(k2,v2);
        }



    }


    /*
     * Hello world
     * Hello you
     * */

}
